﻿Imports System.Collections.Generic
Imports System.ComponentModel
Imports MySql.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports MySql.Data.MySqlClient 'MySql.Data.MySqlClient

Public Class frmEditarUsuario

    Private Sub frmEditarUsuario_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        ' TODO: esta linha de código carrega dados na tabela 'sCE_ETEC_DataSet.Login'. Você pode movê-la ou removê-la conforme necessário.
        Me.loginTableAdapter.Fill(Me.sCE_ETEC_DataSet.Login)
        Try
            For i As Integer = 0 To dgvLogin.Rows.Count - 1
                cbCodigo.Items.Add(dgvLogin.Rows(i).Cells(0).Value.ToString())
                cbNome.Items.Add(dgvLogin.Rows(i).Cells(1).Value.ToString())
                cbUsuario.Items.Add(dgvLogin.Rows(i).Cells(2).Value.ToString())
            Next
        Catch erro As Exception
            MsgBox(erro.Message, vbOKOnly, "Erro")
        Finally
            cbCodigo.SelectedItem = 0
            cbNome.SelectedIndex = 0
            cbUsuario.SelectedIndex = 0
        End Try
    End Sub

    Private Sub cbNome_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbNome.SelectedIndexChanged
        cbCodigo.SelectedIndex = cbNome.SelectedIndex
        cbUsuario.SelectedIndex = cbNome.SelectedIndex
    End Sub

    Private Sub btnEditar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEditar.Click
        ckbAdminstrador.Enabled = True
        txtSenha.Enabled = True
        btnSalvar.Enabled = True
    End Sub

    Private Sub btnSalvar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSalvar.Click
        Dim admin As Integer = 0
        If ckbAdminstrador.Checked = True Then
            admin = 1
        End If

        Dim SCE As New MySqlConnection(My.MySettings.Default.SCE_ETEC_ConnectionString) 'Properties.Settings.[Default].SCE_ETEC_ConnectionString)
        Try
            'SqlConnection SCE = new MySqlConnection(Properties.Settings.Default.SCE_ETEC_ConnectionString);
            Dim cmd As New MySqlCommand()
            SCE.Open()

            ' comando UPDATE tabela SET campo1='valor', campo2='valor' WHERE id='valor'
            Dim sqlCmd As String = "UPDATE Login SET senha=@nova_senha, nivel_acesso=@novo_acesso WHERE cod_usuario=@codigo_usuario"
            cmd = New MySqlCommand(sqlCmd, SCE)

            cmd.Parameters.AddWithValue("@codigo_usuario", cbCodigo.SelectedItem)
            cmd.Parameters.AddWithValue("@nova_senha", txtSenha.Text)
            cmd.Parameters.AddWithValue("@novo_acesso", admin)

            cmd.ExecuteNonQuery()
            MsgBox("Concluido.", vbOKOnly, "Aviso")
        Catch erro As MySqlException
            MsgBox(erro.Message, vbOKOnly, "Erro")
        Finally
            SCE.Close()
            ckbAdminstrador.Checked = False
            txtSenha.Clear()
            ckbAdminstrador.Enabled = False
            txtSenha.Enabled = False
            btnSalvar.Enabled = False
        End Try

    End Sub
End Class